import numpy as np
np.random.seed(123)
Arr=np.random.randn(8,10)금융공학프로그래밍3, Quiz5
Problem 1
- Create a 2-dimensional ndarray object ‘Arr’ with the code below and answer the following.
- Write an expression to calculate column sum of Arr.
np.sum(Arr,axis=0)array([-3.68648899, -3.20797062, 1.89628963, -4.31697028, -1.68652947,
3.31014718, 3.61439765, 2.82609217, 1.68894737, 0.84241575])
- Write an expression that finds the position (row and column index) of an element greater than 2 in Arr
np.where(Arr>2)(array([1, 1, 4, 4]), array([6, 7, 6, 9]))
Problem 2
Write 3 different expressions to create a pandas Series object ‘S’ displayed as follows.
>>> S
a 0
b 1
c 2
d 3
dtype: int64
import pandas as pd
S=pd.Series([0,1,2,3],index=['a','b','c','d'])
Sa 0
b 1
c 2
d 3
dtype: int64
S=pd.Series(dict(a=0,b=1,c=2,d=3))
Sa 0
b 1
c 2
d 3
dtype: int64
S=pd.Series(np.arange(4),index=['a','b','c','d'])
Sa 0
b 1
c 2
d 3
dtype: int64
Problem 3
- Write 4 different expressions to select the 2nd and the 3rd elements of Snew.
Snew = pd.Series({'a': 1, 'b':4, 'c':2, 'd':3} )Snew[1:3]b 4
c 2
dtype: int64
Snew.iloc[[1,2]]b 4
c 2
dtype: int64
Snew['b':'c']b 4
c 2
dtype: int64
Snew[['b','c']]b 4
c 2
dtype: int64
Problem 4~5
np.random.seed(123)
DF = pd.DataFrame(np.random.randn(6,7),
columns=list('abcdefg'),
index=[3,2,4,5,1,0])
DF| a | b | c | d | e | f | g | |
|---|---|---|---|---|---|---|---|
| 3 | -1.085631 | 0.997345 | 0.282978 | -1.506295 | -0.578600 | 1.651437 | -2.426679 |
| 2 | -0.428913 | 1.265936 | -0.866740 | -0.678886 | -0.094709 | 1.491390 | -0.638902 |
| 4 | -0.443982 | -0.434351 | 2.205930 | 2.186786 | 1.004054 | 0.386186 | 0.737369 |
| 5 | 1.490732 | -0.935834 | 1.175829 | -1.253881 | -0.637752 | 0.907105 | -1.428681 |
| 1 | -0.140069 | -0.861755 | -0.255619 | -2.798589 | -1.771533 | -0.699877 | 0.927462 |
| 0 | -0.173636 | 0.002846 | 0.688223 | -0.879536 | 0.283627 | -0.805367 | -1.727669 |
- Fill in the appropriate expression in square brackets so you can select columns ‘c’ and ‘e’.
DF.iloc[:,2:5:2]| c | e | |
|---|---|---|
| 3 | 0.282978 | -0.578600 |
| 2 | -0.866740 | -0.094709 |
| 4 | 2.205930 | 1.004054 |
| 5 | 1.175829 | -0.637752 |
| 1 | -0.255619 | -1.771533 |
| 0 | 0.688223 | 0.283627 |
DF[['c','e']]| c | e | |
|---|---|---|
| 3 | 0.282978 | -0.578600 |
| 2 | -0.866740 | -0.094709 |
| 4 | 2.205930 | 1.004054 |
| 5 | 1.175829 | -0.637752 |
| 1 | -0.255619 | -1.771533 |
| 0 | 0.688223 | 0.283627 |
- Fill in the appropriate expression in square brackets so that you can select rows with negative values for column ‘c’.
DF.loc[DF.c<0,:]| a | b | c | d | e | f | g | |
|---|---|---|---|---|---|---|---|
| 2 | -0.428913 | 1.265936 | -0.866740 | -0.678886 | -0.094709 | 1.491390 | -0.638902 |
| 1 | -0.140069 | -0.861755 | -0.255619 | -2.798589 | -1.771533 | -0.699877 | 0.927462 |
DF[DF.c<0]| a | b | c | d | e | f | g | |
|---|---|---|---|---|---|---|---|
| 2 | -0.428913 | 1.265936 | -0.866740 | -0.678886 | -0.094709 | 1.491390 | -0.638902 |
| 1 | -0.140069 | -0.861755 | -0.255619 | -2.798589 | -1.771533 | -0.699877 | 0.927462 |